package com.kd.d5000acquire.server;


import com.kd.d5000acquire.D5000AcquireApplication;
import com.kd.d5000acquire.util.RequiredFieldValidator;
import com.kd.d5000acquire.util.SendData;
import com.kd.d5000acquire.util.XmlUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.kd.d5000acquire.util.PinyinTransfer.ToFirstChar;
import static com.kd.d5000acquire.util.PinyinTransfer.ToPinyin;


@Component
@Slf4j
@Data
public class AppStatusServer {

    @Resource(name = "myMessageQueue")
    private Queue queue;

    @Value("${my.alarmtransfer.host}")
    public String host;

    @Value("${my.alarmtransfer.port}")
    public int port;


    public static Map<String,Map<String,String>> redisHash = D5000AcquireApplication.redisHash;
    final String aredWAMS="WAMS_GDTC_";  //例QZLL_GDTR_AH.ZDGF-ANHUI.ZHONGDIANGUANGFU 中电光伏
    final String aredWAMS1="WAMS_GDTC2_";
    final String aredWAMS2="WAMS_GDTC3_";
    final String aredQZLL="QZLL_NWTDTC_";
    //告警存储示例 NR_ALARM_QZ_GDTR_**-**    AH.ZDGF
    public void parsingXml(String xml,String tableName,String definitionId) {
        log.info("接受到xml为" + xml);
        Map<String, String> map = XmlUtil.transformXml(xml);
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        Date date = null;
        try {
            date = sdf.parse(map.get("OCCUR_TIME"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        map.put("OCCUR_TIME",sdf.format(date));
        map.put("TABLE_NAME",tableName);
        map.put("DEFINITION_ID",definitionId);
        /**
         * 告警节点提取
         */
        String content1="";
        String content2 = "";
        String definitionId2="";
        String definitionId3="";
        try{
            content1 = map.get("CONTENT").toString();
            String[] arr = content1.split("\\s+");
            for(int j = 2 ;j < arr.length; j++){
                content2 += arr[j]+" " ;
            }
            map.put("CONTENT",content2);

            if(tableName.trim().equals("channel_gk") || tableName.trim().equals("wfes_channel_gk") || tableName.trim().equals("accident_info")){
                System.out.println("链路告警节点："+arr[2]);
                tableName = arr[2];
                map.put("TABLE_NAME",tableName);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        /*告警发送*/
        //非链路状态，发通知
        String regex="";
        String key="";
        Map<String, String> nodeM = null;
        Map<String, String> nodeM2 = null;
       if(!(definitionId.equals("100380000000000141") || definitionId.equals("100380000000000082"))){
           //应用告警分类
           if(definitionId.equals("100380000000000153")){
               regex= "退出";
               Pattern p = Pattern.compile(regex);
               Matcher m = p.matcher(content2);
               while(m.find()){
                   log.info("|"+m.group()+"|");
                   definitionId="100380000000000001";
               }
               regex="断网";
               Pattern p2 = Pattern.compile(regex);
               Matcher m2 = p2.matcher(content2);
               while(m2.find()){
                   log.info("|"+m.group()+"|");
                   definitionId="100380000000000003";
               }
               regex="工作站工作站";
               content2=content2.replaceAll("-ws.+?系统", "工作站工作站");
               Pattern p3 = Pattern.compile(regex);
               Matcher m3 = p3.matcher(content2);
               while(m3.find()){
                   definitionId="";
               }
           }
           if(RequiredFieldValidator.NotNullAndNotEmptyAndNotEqNull(definitionId)){
               //int i = SendData.sendNoticeAlarmData("10031d", sdf.format(date), "-", tableName, Arrays.asList(map.get("CONTENT")), definitionId, host, port);
               int i= SendData.sendAlarmData("10031d", sdf.format(date), "-", tableName, Arrays.asList(content2),definitionId, host, port);
           }
       }else{
           //链路转态，投入与退出发恢复告警
           if(definitionId.equals("100380000000000082")){
               //WAMS链路（PMU)判定，管道投入，退出要区分管道1，管道2
               regex="故障";
               Pattern p4 = Pattern.compile(regex);
               Matcher m4 = p4.matcher(content2);
               while(m4.find()){
                   log.info("|"+m4.group()+"|");
                   definitionId="100380000000000083";
               }

               //WAMS链路（PMU)判定，管道投入，退出要区分管道，管道1，管道2
               final String aredWAMS="WAMS_GDTC_";  //例QZLL_GDTR_AH.ZDGF-ANHUI.ZHONGDIANGUANGFU 中电光伏
               final String aredWAMS2="WAMS_GDTC2_";
               final String aredWAMS3="WAMS_GDTC3_";
               regex="退出";
               Pattern p5 = Pattern.compile(regex);
               Matcher m5 = p5.matcher(content2);
               while(m5.find()) {
                   log.info("|" + m5.group() + "|");
                   definitionId = "100380000000000085";
                   regex = "管道";
                   Pattern p6 = Pattern.compile(regex);
                   Matcher m6 = p6.matcher(content2);
                   while (m6.find()) {
                       log.info("|" + m6.group() + "|");
                       if (redisHash.containsKey(aredWAMS + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))) {
                           if (nodeM.containsValue("1")) {
                               definitionId3 = "100380000000000084";
                               key = aredWAMS + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                               nodeM2.put("OCCUR_TIME", map.get("OCCUR_TIME"));
                               nodeM2.put("RECOVER_TIME", "");
                               nodeM2.put("TABLE_NAME", tableName);
                               nodeM2.put("CONTENT", content2);
                               nodeM2.put("RECOVER_CONTENT", "");
                               nodeM2.put("FLAG", "-1");
                               nodeM2.put("DEFINITION_ID", definitionId3);
                               redisHash.put(key, nodeM2);
                           }
                       } else {
                           definitionId2 = "100380000000000084";
                           key = aredWAMS + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                           nodeM2.put("OCCUR_TIME", map.get("OCCUR_TIME"));
                           nodeM2.put("RECOVER_TIME", "");
                           nodeM2.put("TABLE_NAME", tableName);
                           nodeM2.put("CONTENT", content2);
                           nodeM2.put("RECOVER_CONTENT", "");
                           nodeM2.put("FLAG", "-1");
                           nodeM2.put("DEFINITION_ID", definitionId2);
                           redisHash.put(key, nodeM2);
                       }
                   }

                   regex = "管道1";
                   Pattern p7 = Pattern.compile(regex);
                   Matcher m7 = p7.matcher(content2);
                   while (m7.find()) {
                       log.info("|" + m7.group() + "|");
                       if (redisHash.containsKey(aredWAMS1 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))) {
                           if (nodeM.containsValue("1")) {
                               definitionId3 = "100380000000000084";
                               key = aredWAMS1 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                               nodeM2.put("OCCUR_TIME", map.get("OCCUR_TIME"));
                               nodeM2.put("RECOVER_TIME", "");
                               nodeM2.put("TABLE_NAME", tableName);
                               nodeM2.put("CONTENT", content2);
                               nodeM2.put("RECOVER_CONTENT", "");
                               nodeM2.put("FLAG", "-1");
                               nodeM2.put("DEFINITION_ID", definitionId3);
                               redisHash.put(key, nodeM2);
                           }
                       } else {
                           definitionId2 = "100380000000000084";
                           key = aredWAMS1 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                           nodeM2.put("OCCUR_TIME", map.get("OCCUR_TIME"));
                           nodeM2.put("RECOVER_TIME", "");
                           nodeM2.put("TABLE_NAME", tableName);
                           nodeM2.put("CONTENT", content2);
                           nodeM2.put("RECOVER_CONTENT", "");
                           nodeM2.put("FLAG", "-1");
                           nodeM2.put("DEFINITION_ID", definitionId2);
                           redisHash.put(key, nodeM2);
                       }
                   }


                   regex = "管道2";
                   Pattern p8 = Pattern.compile(regex);
                   Matcher m8 = p8.matcher(content1);
                   while (m8.find()) {
                       log.info("|" + m8.group() + "|");
                       if (redisHash.containsKey(aredWAMS2 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))) {
                           if (nodeM.containsValue("1")) {
                               definitionId3 = "100380000000000084";
                               key = aredWAMS2 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                               nodeM2.put("OCCUR_TIME", map.get("OCCUR_TIME"));
                               nodeM2.put("RECOVER_TIME", "");
                               nodeM2.put("TABLE_NAME", tableName);
                               nodeM2.put("CONTENT", content2);
                               nodeM2.put("RECOVER_CONTENT", "");
                               nodeM2.put("FLAG", "-1");
                               nodeM2.put("DEFINITION_ID", definitionId3);
                               redisHash.put(key, nodeM2);

                           }
                       } else {
                           definitionId2 = "100380000000000084";
                           key = aredWAMS2 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                           nodeM2.put("OCCUR_TIME", map.get("OCCUR_TIME"));
                           nodeM2.put("RECOVER_TIME", "");
                           nodeM2.put("TABLE_NAME", tableName);
                           nodeM2.put("CONTENT", content2);
                           nodeM2.put("RECOVER_CONTENT", "");
                           nodeM2.put("FLAG", "-1");
                           nodeM2.put("DEFINITION_ID", definitionId2);
                           redisHash.put(key, nodeM2);
                       }
                   }
               }

               regex="投入";
               Pattern p52 = Pattern.compile(regex);
               Matcher m52 = p52.matcher(content2);
               while(m52.find()){
                   log.info("|" + m52.group() + "|");
                   definitionId = "100380000000000086";
                   regex="管道";
                   Pattern p62 = Pattern.compile(regex);
                   Matcher m62 = p62.matcher(content2);
                   while(m62.find()) {
                       log.info("|" + m62.group() + "|");
                       if (redisHash.containsKey(aredWAMS + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))) {
                           if (nodeM.containsValue("-1")) {
                               definitionId3 = "100380000000000084";
                               key = aredWAMS + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName);
                               nodeM2.put("OCCUR_TIME", nodeM.get("OCCUR_TIME"));
                               nodeM2.put("RECOVER_TIME", map.get("OCCUR_TIME"));
                               nodeM2.put("TABLE_NAME", tableName);
                               nodeM2.put("CONTENT", nodeM.get("CONTENT"));
                               nodeM2.put("RECOVER_CONTENT", content2);
                               nodeM2.put("FLAG", "1");
                               nodeM2.put("DEFINITION_ID", definitionId2);
                               redisHash.put(key, nodeM2);
                           }
                       }
                   }

                   regex="管道1";
                   Pattern p72 = Pattern.compile(regex);
                   Matcher m72 = p72.matcher(content1);
                   while(m72.find()){
                       log.info("|"+m72.group()+"|");
                       if(redisHash.containsKey(aredWAMS1 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))){
                           if(nodeM.containsValue("-1")){
                               definitionId3="100380000000000084";
                               key=aredWAMS1+ToFirstChar(tableName).toUpperCase()+"-"+ToPinyin(tableName);
                               nodeM2.put("OCCUR_TIME",nodeM.get("OCCUR_TIME"));
                               nodeM2.put("RECOVER_TIME",map.get("OCCUR_TIME"));
                               nodeM2.put("TABLE_NAME",tableName);
                               nodeM2.put("CONTENT",nodeM.get("CONTENT"));
                               nodeM2.put("RECOVER_CONTENT",content2);
                               nodeM2.put("FLAG","1");
                               nodeM2.put("DEFINITION_ID",definitionId3);
                               redisHash.put(key,nodeM2);
                           }
                       }
                   }

                   regex="管道2";
                   Pattern p82 = Pattern.compile(regex);
                   Matcher m82 = p82.matcher(content1);
                   while(m82.find()){
                       log.info("|"+m82.group()+"|");
                       if(redisHash.containsKey(aredWAMS2 + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))){
                           if(nodeM.containsValue("-1")){
                               definitionId3="100380000000000084";
                               key=aredWAMS2+ToFirstChar(tableName).toUpperCase()+"-"+ToPinyin(tableName);
                               nodeM2.put("OCCUR_TIME",nodeM.get("OCCUR_TIME"));
                               nodeM2.put("RECOVER_TIME",map.get("OCCUR_TIME"));
                               nodeM2.put("TABLE_NAME",tableName);
                               nodeM2.put("CONTENT",nodeM.get("CONTENT"));
                               nodeM2.put("RECOVER_CONTENT",content2);
                               nodeM2.put("FLAG","1");
                               nodeM2.put("DEFINITION_ID",definitionId3);
                               redisHash.put(key,nodeM2);
                           }
                       }
                   }
               }
           }

           /*前置链路*/
           if(definitionId.equals("100380000000000141")){
               //前置链路故障，遥测不刷新
               regex="故障,遥测不刷新";
               Pattern p5 = Pattern.compile(regex);
               Matcher m5 = p5.matcher(content1);
               while(m5.find()){
                   log.info("|"+m5.group()+"|");
                   definitionId="100380000000000142";
               }

               //退出
               regex="退出";
               Pattern p6 = Pattern.compile(regex);
               Matcher m6 = p6.matcher(content1);
               while(m6.find()){
                   log.info("|"+m6.group()+"|");
                   definitionId="100380000000000144";
                   if(redisHash.containsKey(aredQZLL + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))){
                       if(nodeM.containsValue("1")){
                           definitionId3="100380000000000143";
                           key=aredQZLL+ToFirstChar(tableName).toUpperCase()+"-"+ToPinyin(tableName);
                           nodeM2.put("OCCUR_TIME",map.get("OCCUR_TIME"));
                           nodeM2.put("RECOVER_TIME","");
                           nodeM2.put("TABLE_NAME",tableName);
                           nodeM2.put("CONTENT",content2);
                           nodeM2.put("RECOVER_CONTENT","");
                           nodeM2.put("FLAG","-1");
                           nodeM2.put("DEFINITION_ID",definitionId3);
                           redisHash.put(key,nodeM2);
                       }
                   }else {
                       definitionId2="100380000000000143";
                       key=aredQZLL+ToFirstChar(tableName).toUpperCase()+"-"+ToPinyin(tableName);
                       nodeM2.put("OCCUR_TIME",map.get("OCCUR_TIME"));
                       nodeM2.put("RECOVER_TIME","");
                       nodeM2.put("TABLE_NAME",tableName);
                       nodeM2.put("CONTENT",content2);
                       nodeM2.put("RECOVER_CONTENT","");
                       nodeM2.put("FLAG","-1");
                       nodeM2.put("DEFINITION_ID",definitionId2);
                       redisHash.put(key,nodeM2);
                   }
               }

               //投入
               regex="投入";
               Pattern p7 = Pattern.compile(regex);
               Matcher m7 = p7.matcher(content1);
               while(m7.find()){
                   log.info("|"+m7.group()+"|");
                   definitionId="100380000000000145";
                   if(redisHash.containsKey(aredQZLL + ToFirstChar(tableName).toUpperCase() + "-" + ToPinyin(tableName))){
                       if(nodeM.containsValue("-1")){
                           definitionId3="100380000000000143";
                           key=aredQZLL+ToFirstChar(tableName).toUpperCase()+"-"+ToPinyin(tableName);
                           nodeM2.put("OCCUR_TIME",nodeM.get("OCCUR_TIME"));
                           nodeM2.put("RECOVER_TIME",map.get("OCCUR_TIME"));
                           nodeM2.put("TABLE_NAME",tableName);
                           nodeM2.put("CONTENT",nodeM.get("CONTENT"));
                           nodeM2.put("RECOVER_CONTENT",content2);
                           nodeM2.put("FLAG","1");
                           nodeM2.put("DEFINITION_ID",definitionId3);
                           redisHash.put(key,nodeM2);
                       }
                   }
               }
           }

           /*map.put("DEFINITION_ID",definitionId);
           String s = XmlUtil.generateAppStatusXml(map);
           log.info("转换为:\n"+ s);*/
           //int i = SendData.sendNoticeAlarmData("10031d", sdf.format(date), "-", tableName, Arrays.asList(content2), definitionId, host, port);
           //发一般告警
           if(RequiredFieldValidator.NotNullAndNotEmptyAndNotEqNull(definitionId)){
               int i= SendData.sendAlarmData("10031d", sdf.format(date), "-", tableName, Arrays.asList(content2),definitionId, host, port);
           }
           //发紧急告警
           if(RequiredFieldValidator.NotNullAndNotEmptyAndNotEqNull(definitionId2)){
               int i2= SendData.sendAlarmData("10031d", sdf.format(date), "-", tableName, Arrays.asList(content2),definitionId2, host, port);
           }
           //发恢复告警
           if(RequiredFieldValidator.NotNullAndNotEmptyAndNotEqNull(definitionId3)){
               int i3= SendData.sendDisAlarmData("10031d", nodeM2.get("OCCUR_TIME"), sdf.format(date), tableName, Arrays.asList(nodeM2.get("CONTENT")), definitionId3, host, port);
           }
       }
    }
}